using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Workflow.ComponentModel.Compiler;
using OpenComposite.EII.Repository;
using OpenComposite.Base.Collections;

namespace OpenComposite.EII.Designer.Workflow
{
	public partial class FormSelectParameterResolve : Form
	{
		#region Initialization
		private FormSelectParameterResolve()
		{

			_lstExcludedPropNames = new List<string>();
			_lstExcludedPropNames.Add("SimulateAllLogicalActivities");
			_lstExcludedPropNames.Add("IsSimulation");
			_lstExcludedPropNames.Add("IsDocumentFlowSimulation");
			_lstExcludedPropNames.Add("InstanceGuid");
			_lstExcludedPropNames.Add("InstanceId");

			_lstExcludedPropNamesEndsWith = new List<string>();
			_lstExcludedPropNamesEndsWith.Add("EventArgs");

			InitializeComponent();
		}

		public FormSelectParameterResolve(IServiceProvider serviceProvider,
			Repository.Workflow workflow, BindingListEx<TreeNode> nodes)
			: this()
		{
			_serviceProvider = serviceProvider;
			_workflow = workflow;
			treeParamResolve.Nodes.AddRange(nodes.ToArray());
			treeParamResolve.CollapseAll();
		}
		#endregion

		#region Public Members

		public List<string> ExcludedPropertyNames
		{
			get { return _lstExcludedPropNames; }
		}
		public List<string> ExcludedPropertyNamesEndsWith
		{
			get { return _lstExcludedPropNamesEndsWith; }
		}

		public string SelectedPath
		{
			get { return treeParamResolve.SelectedNode != null ? getNodePath(treeParamResolve.SelectedNode) : _path; }
			set
			{
				_path = value;
				if ( !string.IsNullOrEmpty(value) ) {
					treeParamResolve.SelectedNode = findPath(_path);
				}
			}
		}

		#endregion

		#region Private Members

		#region Variables
		private IServiceProvider _serviceProvider;
		//private string _WFClassFullName;
		//private Type _typeWFClass;
		private string _path = "";
		private List<string> _lstExcludedPropNames;
		private List<string> _lstExcludedPropNamesEndsWith;
		private Repository.Workflow _workflow;
		#endregion

		#region Methods
		private string getNodePath(TreeNode node)
		{
			if ( node == null ) return null; // EXIT
			if ( node.Parent == null ) return node.Text; // EXIT
			return getNodePathRecursive(node, "");
		}
		private string getNodePathRecursive(TreeNode node, string path)
		{
			if ( node.Parent != null ) {
				path = getNodePathRecursive(node.Parent, path);
				if ( node.Parent/*.Parent*/ != null ) {
					path = string.Format("{0}.", path);
				}
			}
			return string.Format("{0}{1}", path, node.Text);
		}

		private TreeNode findPath(string path)
		{
			string[] items = path.Split('.');
			TreeNodeCollection nodes = treeParamResolve.Nodes;
			TreeNode target = null;
			for ( int i = 0; i < items.Length; i++ ) {
				foreach ( TreeNode node in nodes ) {
					if ( node.Text == items[i] ) {
						if ( node.Nodes == null || node.Nodes.Count == 0 ) return node;
						target = node;
						nodes = node.Nodes;
						break;
					}
				}
			}
			return target;
		}

		#endregion

		#region Event Handlers
		private void treeParamResolve_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if ( e.Node.Bounds.Contains(PointToClient(TreeView.MousePosition)) && e.Node.Tag != null ) {
				this.DialogResult = DialogResult.OK;
				this.Close();
			}
			//this.Close();
		}
		private void treeParamResolve_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
		{
			if ( e.KeyCode == Keys.Escape ) {
				this.DialogResult = DialogResult.Cancel;
				this.Close();
			}
		}

		private void tsbtnOK_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
			this.Close();
		}
		private void tsbtnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		#endregion

		private void treeParamResolve_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if ( e.Node.Tag == null || e.Node.ForeColor == Color.Gray ) e.Cancel = true;
		}


		#endregion

		private void FormSelectParameterResolve_FormClosed(object sender, FormClosedEventArgs e)
		{
			_path = getNodePath(treeParamResolve.SelectedNode);
			treeParamResolve.Nodes.Clear();

		}

	}
}